package cn.elves.model.activity.service.impl;

import cn.elves.model.activity.entity.po.Activity;
import cn.elves.model.activity.entity.po.UserTakeActivity;
import cn.elves.model.activity.entity.po.UserTakeActivityCount;
import cn.elves.model.activity.mapper.UserTakeActivityMapper;
import cn.elves.model.activity.service.ActivityService;
import cn.elves.model.activity.service.UserTakeActivityCountService;
import cn.elves.model.activity.service.UserTakeActivityService;
import cn.elves.model.rule.service.RuleTreeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author pc
 * @description 针对表【user_take_activity】的数据库操作Service实现
 * @createDate 2024-06-17 00:09:04
 */
@Service
public class UserTakeActivityServiceImpl extends ServiceImpl<UserTakeActivityMapper, UserTakeActivity>
        implements UserTakeActivityService {

    @Resource
    private UserTakeActivityMapper userTakeActivityMapper;

    @Resource
    private UserTakeActivityCountService userTakeActivityCountService;

    @Resource
    private ActivityService activityService;

    @Resource
    private RuleTreeService ruleTreeService;

    @Override
    public UserTakeActivity selectUnusedBillForUser(Long userId, Long activityId) {
        QueryWrapper<UserTakeActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("state", 0);
        queryWrapper.orderByDesc("id");
        List<UserTakeActivity> userTakeActivities = userTakeActivityMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(userTakeActivities)) {
            return null;
        }
        return userTakeActivities.get(0);
    }

    @Override
    public Boolean joinActivity(Long userId, Activity activity, Map<String, Object> matter) {


        UserTakeActivityCount userTakeActivityCount = userTakeActivityCountService.selectActivityTakeCount(userId, activity.getId());
        if (userTakeActivityCount == null) {
            userTakeActivityCount = userTakeActivityCountService.createUserTakeActivityCount(userId, activity.getId());
        }

        //扣减库存
        Boolean decreaseSuccess = activityService.decreaseStockCount(activity.getId());
        Boolean takeSuccess = userTakeActivityCountService.increaseLeftCount(userTakeActivityCount);
        if (!takeSuccess) {
            return false;
        }

        //根据条件利用规则引擎筛选策略
        String strategy = ruleTreeService.doDecision(activity.getTreeId(), matter);
        if(StringUtils.isBlank(strategy)){
            throw new RuntimeException("非法策略");
        }

        //根据策略插入当前情况
        Integer leftCount = userTakeActivityCount.getLeftCount() + 1;
        UserTakeActivity userTakeActivity = new UserTakeActivity();
        userTakeActivity.setActivityId(activity.getId());
        userTakeActivity.setTakeDate(new Date());
        userTakeActivity.setState(0);
        userTakeActivity.setTakeCount(leftCount);
        userTakeActivity.setStrategyId(Long.parseLong(strategy));
        userTakeActivityMapper.insert(userTakeActivity);


        return decreaseSuccess;
    }
}




